package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-30 11:48
 */
@RestController
@RequestMapping("saleListGoods")
public class SaleListController {

    @Autowired
    private SaleListService saleListService;

    @Autowired
    private Gson gson;

    //http://localhost:8080/saleListGoods/save?saleNumber=XS1605772263999
    //销售单保存
    @PostMapping("save")
    public ServiceVO save(@RequestParam("saleNumber") String saleNumber,
                          SaleList saleList,
                          String saleListGoodsStr,
                          String customerName,
                          HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();

        List<SaleListGoods> saleListGoodsList = gson.fromJson(saleListGoodsStr, new TypeToken<List<SaleListGoods>>() {}.getType());

        saleList.setUserId(userId);
        saleList.setSaleNumber(saleNumber);
        saleList.setCustomerName(customerName);

        return saleListService.saleListGoodsListSave(saleList,saleListGoodsList);
    }

    //条件查询
    //http://localhost:8080/saleListGoods/list
    @PostMapping("list")
    public Map<String,Object> list(@Param("saleNumber") String saleNumber,
                                   @Param("customerId") Integer customerId,
                                   @Param("state") Integer state,
                                   @Param("sTime") String sTime,
                                   @Param("eTime") String eTime){

        return saleListService.getSaleSearchList(saleNumber,customerId,state,sTime,eTime);
    }


    //查询进货单商品信息
    //http://localhost:8080/saleListGoods/list
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(@Param("saleListId") Integer saleListId){

        return saleListService.getSaleGoodsList(saleListId);
    }


    //http://localhost:8080/saleListGoods/delete
    //进货单删除
    @PostMapping("delete")
    public ServiceVO delete(@Param("saleListId") Integer saleListId){
        return saleListService.deleteSaleListGoods(saleListId);
    }

    //支付结算（修改进货单付款状态）
    //http://localhost:8080/saleListGoods/updateState
    @PostMapping("updateState")
    public ServiceVO updateState(@Param("saleListId") Integer saleListId){
        return saleListService.updateSaleListGoods(saleListId);
    }
}
